﻿#region Opis pliku
//-----------------------------------------------------------------------------
// GamePlayScreen.cs
// Autor: Jan Czarnowski
// 6.11.2012
//-----------------------------------------------------------------------------
#endregion

#region Using...

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Content;
using System.Collections.Generic;
using System;
using Microsoft.Xna.Framework.Input;

#endregion

namespace pogra
{
    /// <summary>
    /// Klasa ekranu rozgrywki. Wyświetla mapę oraz jednostki za pośrednictwem EntityManager.
    /// Wyświetlaniem interfejsu zajmuje się GamePlayGUI
    /// </summary>
    class GamePlayScreen : GameScreen
    {
        #region pola

        Map map;
        ContentManager content;
        Texture2D tanktexA, tanktexB, grunttex, factorytex, barracktex, particletexture, maptexture;
        EntityManager entitymanager;
        Point mousePressedPos, mousePos;
        bool leftMouseButtonPressed;
        ExtendedSpriteBatch exSpriteBatch;

        public EntityManager EntityManager
        {
            get { return entitymanager; }
        }

        public Map Map
        {
            get { return map; }
        }

        #endregion

        #region inicjalizacja

        public GamePlayScreen()
        {
        }

        public override void LoadContent()
        {

            map = new Map();

            content = new ContentManager(ScreenManager.Game.Services, "Content");
            tanktexA = content.Load<Texture2D>("czolg2");
            tanktexB = content.Load<Texture2D>("heli");
            factorytex = content.Load<Texture2D>("fabryka");
            grunttex = content.Load<Texture2D>("grunt");
            maptexture = content.Load<Texture2D>("trawa");
            barracktex = content.Load<Texture2D>("koszary");
            particletexture = content.Load<Texture2D>("menuparticle1");
            entitymanager = new EntityManager(tanktexA, tanktexB, grunttex, factorytex, barracktex, particletexture);
            exSpriteBatch = ScreenManager.ExtendedSpriteBatch;

            map.Load("mapa1.txt",maptexture);
        }

        #endregion

        #region metody draw i update

        public override void Draw(GameTime gameTime)
        {
            ScreenManager.Game.GraphicsDevice.Clear(Color.Green);
            entitymanager.Draw(exSpriteBatch);
            
            // jeśli zaznaczamy to rysuj prostokąt
            if (leftMouseButtonPressed == true && (Math.Abs(mousePos.Y - mousePressedPos.Y) > 5 || Math.Abs(mousePos.X - mousePressedPos.X) > 5))
                exSpriteBatch.DrawRectangle(new Rectangle(mousePressedPos.X, mousePressedPos.Y, mousePos.X - mousePressedPos.X, mousePos.Y - mousePressedPos.Y), Color.Aquamarine,2);
            
            map.Draw(exSpriteBatch);
        }

        public override void Update(GameTime gameTime)
        {
            entitymanager.Update(gameTime);
        }

        #endregion

        #region metody publiczne

        public override void HandleInput(InputState input, bool otherScreenHasFocus, ref bool eventHandled)
        {
            if (eventHandled)
                return;

            RTSGame game = ((RTSGame)ScreenManager.Game);

            if (input.IsNewMLeftButtonPressed() && 
                entitymanager.CanSelect)
            {                
                mousePressedPos = game.Camera.transformPoint(new Point(input.CurrentMouseState.X, input.CurrentMouseState.Y), game.GraphicsDevice);

                leftMouseButtonPressed = true;
            }            
            else

            if (input.IsNewMRightButtonPressed())
            {
                mousePos = game.Camera.transformPoint(new Point(input.CurrentMouseState.X, input.CurrentMouseState.Y), game.GraphicsDevice);
                entitymanager.HandleMouse(mousePos, mousePos, false);
            }
            else


            if (input.WasMLeftButtonReleased())
            {

                leftMouseButtonPressed = false;
                entitymanager.HandleMouse(mousePressedPos, mousePos, true);
            }

            // aktualna pozycja myszki - do rysowania prostokoąta w metodzie draw
            if (leftMouseButtonPressed)
            {
                mousePos = game.Camera.transformPoint(new Point(input.CurrentMouseState.X, input.CurrentMouseState.Y), game.GraphicsDevice);
            }


        }

        #endregion
    }
}
